package com.lzh.chatnote.chat.service.adapter;

import com.lzh.chatnote.chat.domain.entity.Message;
import com.lzh.chatnote.chat.domain.vo.request.message.MessageSendReq;
import com.lzh.chatnote.chat.domain.vo.response.message.MessageResp;
import com.lzh.chatnote.chat.service.strategy.AbstractMsgHandler;
import com.lzh.chatnote.chat.service.strategy.MsgFactoryHandler;
import lombok.Data;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
public class MessageAdapter {

    public static List<MessageResp> buildMsgResp(List<Message> messageList, Long receiveUid) {
        return messageList.stream().map(message -> {
            MessageResp messageResp = new MessageResp();
            messageResp.setUserInfo(buildUserInfo(message));
            messageResp.setMessageInfo(buildMessageInfo(message));
            //todo 点赞，举报，取消 receiveUid
            return messageResp;
        }).collect(Collectors.toList());
    }

    public static MessageResp.UserInfo buildUserInfo(Message message) {
        MessageResp.UserInfo userInfo = new MessageResp.UserInfo();
        userInfo.setUserId(message.getFromUid());
        return userInfo;
    }

    private static MessageResp.MessageInfo buildMessageInfo(Message message) {
        MessageResp.MessageInfo messageInfo = new MessageResp.MessageInfo();
        messageInfo.setId(message.getId());
        messageInfo.setType(message.getType());
        messageInfo.setSeedTime(message.getCreateTime());
        messageInfo.setRoomId(message.getRoomId());
        //通过工厂模式获取消息处理类的运行类型
        AbstractMsgHandler abstractMsgHandler = MsgFactoryHandler.getStrategyNoNull(message.getType());
        if (Objects.nonNull(abstractMsgHandler)) {
            //这里使用了设计模式的工厂模式
            messageInfo.setBody(abstractMsgHandler.showMsgJsonObj(message));
        }
        return messageInfo;
    }

    public static Message buildSendMsg(MessageSendReq req, Long uuid) {
        return Message.builder()
                .roomId(req.getRoomId())
                .type(req.getMsgType())
                .fromUid(uuid)
                .build();
    }

    public static MessageResp buildSendMsgResp(Long msgId) {
        return null;
    }
}
